/**
 * @file    app_monitor.c
 * @brief   用户任务-监控任务,监控所有任务是否正常运行，任一个任务未运行则复位系统
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-04-24
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-24 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "app_monitor.h"
#include "If_wdg.h"
#include "FreeRTOS.h"
#include "task.h"
#include "If_do.h"
#include "event_mng.h"
#include "var_mng.h"
/* macro/define --------------------------------------------------------------*/
#define MONITOR_TASK_CYCLE          (100) 
#define MONITOR_TASK_TIMEOUT        (5000)   /* 任务监控超时时间 */
/* typedef -------------------------------------------------------------------*/
typedef struct
{
    uint32_t last_time ;
    uint32_t cycle ;
    uint32_t run_count;
}task_msg_type;
/* local functions -----------------------------------------------------------*/
void If_do_beep_alarm(void);
/* static variables ----------------------------------------------------------*/
static task_msg_type task_count_buf[TASK_MAX_NUM] = {0};
/* global variables ----------------------------------------------------------*/
uint8_t do_id = 0;
/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief 系统监控任务
 * @brief 监控所有任务是否正常运行，如果有任一一个任务超时，则系统停止喂狗，复位重启
*/
void vMonitorTask(void *pvParameters)
{
    uint8_t i = 0;
    uint8_t run_task_num = 0;   // 当前运行任务计数器
	uint16_t unrun_task_dly = 0;  // 当前未运行任务计时器
    portTickType xLastExecutionTime;

    xLastExecutionTime = xTaskGetTickCount();
    
    If_wdg_feed();
    
    while(1)
    {
        run_task_num = 0;
        
        for( i = 0; i < TASK_MAX_NUM ; ++ i)
        {
            if(task_count_buf[i].run_count != 0)
            {
                run_task_num ++ ;
            }
        }
        
        if(run_task_num == TASK_MAX_NUM)
        {
            If_wdg_feed();
            
            for( i = 0; i < TASK_MAX_NUM ; ++ i)
            {
                task_count_buf[i].run_count = 0;
            }
			unrun_task_dly = 0;
        }
		else
		{
			if(unrun_task_dly >= MONITOR_TASK_TIMEOUT)
			{
				//记录事件，系统重启
				unrun_task_dly = MONITOR_TASK_TIMEOUT;
			//	var_set_data(VAR_ID_USE_CMD , USE_CMD_RESET);
			}
			else
			{
				If_wdg_feed();  //新增开门狗复位引起系统启动异常20240309
			}
		}
		
		task_run_count(TASK_ID_MONITOR);
        
        vTaskDelayUntil( &xLastExecutionTime, MONITOR_TASK_CYCLE);
    }
}

void task_run_count(uint8_t task_id)
{
    if(task_id >= TASK_MAX_NUM)
        return ;
    
    task_count_buf[task_id].cycle = xTaskGetTickCount() - task_count_buf[task_id].last_time;
    task_count_buf[task_id].run_count ++ ;
    task_count_buf[task_id].last_time = xTaskGetTickCount();
}

void task_run_reset(uint8_t task_id)
{
    if(task_id >= TASK_MAX_NUM)
        return ;
    
    task_count_buf[task_id].cycle = 0;
    task_count_buf[task_id].run_count ++ ;
    task_count_buf[task_id].last_time = xTaskGetTickCount();
}

void task_printf_cycle(uint8_t com_id)
{
//	char str[50] = {0};
//	uint8_t i = 0;
//	
//	sprintf(str , "Now:%d\r\n" , xTaskGetTickCount());
//	If_com_send_str(com_id , (uint8_t *)str , strlen(str));
//	for( i = 0;i < TASK_MAX_NUM; ++ i)
//	{
//		sprintf(str , "%d:%d\-" , i , task_count_buf[i].run_count);
//		If_com_send_str(com_id , (uint8_t *)str , strlen(str));
//	}
//	If_com_send_str(com_id , "\r\n" , 2);
}

uint32_t task_get_cycle(uint8_t task_id)
{
	if(task_id >= TASK_MAX_NUM)
        return 0;
    
    return task_count_buf[task_id].cycle;
}
/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
 
/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/


